//
//  DYBigPictureViewController.m
//  BaiSi
//
//  Created by mac on 16/11/4.
//  Copyright © 2016年 DeYi. All rights reserved.
//

#import "DYBigPictureViewController.h"
#import <UIImageView+WebCache.h>
#import "DYThemeItem.h"
#import "LHPhotoAlbumManager.h"
#import <SVProgressHUD/SVProgressHUD.h>

@interface DYBigPictureViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property(nonatomic,weak) UIImageView *imageView;
@end

@implementation DYBigPictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //添加图片
    [self setUpImageView];
    
    //缩放
    if (_item.width > DYScreenW) {
        _scrollView.delegate = self;
        _scrollView.maximumZoomScale = _item.width / DYScreenW;
        _scrollView.minimumZoomScale = 1;
    }
}

#pragma mark - UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return  _imageView;
}

#pragma mark - 添加图片
- (void)setUpImageView {
    UIImageView *imageView = [[UIImageView alloc] init];
    _imageView = imageView;
    [_scrollView addSubview:imageView];
    [imageView sd_setImageWithURL:[NSURL URLWithString:_item.image0] placeholderImage:nil options:SDWebImageProgressiveDownload];
    CGFloat h =  DYScreenW / _item.width * _item.height;
    imageView.frame = CGRectMake(0, 0, DYScreenW, h);
    if (_item.is_big) {//大图
        _scrollView.contentSize = CGSizeMake(0, h);
    } else {
        
        imageView.center = CGPointMake(DYScreenW * 0.5, DYScreenH * 0.5);
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 点击事件
- (IBAction)dismissBtn:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)saveBtn:(UIButton *)sender {
    LHLog(@"保存");
    [LHPhotoAlbumManager savePhotoWithAuthorizationStatus:_imageView.image albumTitle:DYPotoAlbumTitle refuseInfoHandle:^{
        
        [SVProgressHUD showInfoWithStatus:@"进入(设置) -> 查找到百思 -> 允许访问相册"];
        
    } completionHandler:^(BOOL success, NSError *error) {
        if (success) {
            [SVProgressHUD showSuccessWithStatus:@"保存成功"];
        }
        if (error) {
            [SVProgressHUD showErrorWithStatus:@"保存失败"];
        }
    }];
}

@end
